﻿// owner 蒋家伟

#include "ObjectReflectFactory.h"

// jsdk
#include "Dbg_Warn_Define.h"

using namespace jsdk;

jsdk::ObjectReflectFactory& jsdk::ObjectReflectFactory::get()
{
    static ObjectReflectFactory s_ObjectReflectFactory;
    return s_ObjectReflectFactory;
}


jsdk::ObjectReflectFactory::~ObjectReflectFactory()
{
}

bool jsdk::ObjectReflectFactory::registerReflectClassDescriptor(const UniqueId& uniqueId, const std::wstring& classTypeName, const reflectFun& reflectfun)
{
    return m_uidToObjectUmap.insert(std::make_pair(uniqueId, NEW_AS_OWNERPTR(ClassDescriptor, uniqueId, classTypeName, reflectfun))).second;
}

jsdk::OwnerPtr<jsdk::DBSchemaObject> jsdk::ObjectReflectFactory::getReflectObject(const UniqueId& uniqueId) const
{
    const auto& iter = m_uidToObjectUmap.find(uniqueId);
    if (iter != m_uidToObjectUmap.end())
    {
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(iter->second, L"注册了类描述但是没有类描述符对象", L"蒋家伟", L"2023-11-20");
        return iter->second->ReflectFun();
    }
    return nullptr;
}

jsdk::FieldReflectManager* jsdk::ObjectReflectFactory::getFieldReflectManager(const UniqueId& uniqueId, void* pClassAddress)
{
    auto iter = m_uidToObjectUmap.find(uniqueId);
    if (iter != m_uidToObjectUmap.end())
    {
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(iter->second, L"注册了类描述但是没有类描述符对象", L"蒋家伟", L"2023-11-20");
        jsdk::FieldReflectManager& fieldReflectManager = iter->second->FieldReflectMgr;
        fieldReflectManager.setClassAddress(pClassAddress);
        return &fieldReflectManager;
    }
    return nullptr;
}

jsdk::ClassDescriptor::ClassDescriptor(const UniqueId& uniqueId, const std::wstring& classTypeName, const reflectFun& reflectfun):
    ReflectFun(reflectfun),
    FieldReflectMgr(uniqueId, classTypeName)
{
}
